# python 3.6.8 
import logging

def readFile(file):
    coding = ['utf-8', 'gbk']
    for i in coding:
        try:
            logging.debug(f'读取文件: {file}')
            with open(file, encoding = i) as f:
                return f.read()
        except UnicodeDecodeError as e:
            logging.debug(f'出现文本文件编码异常: {e}')
    logging.debug(f'文件出现未知的文件编码: {e}')
    return None

def writeFile(file, cont):
    coding = ['utf-8', 'gbk']
    for i in coding:
        try:
            logging.debug(f'写入文件: {file}')
            with open(file, 'w+', encoding = i) as f:
                return f.write(cont)
        except UnicodeDecodeError as e:
            logging.debug(f'出现文本文件编码异常: {e}')
    logging.debug(f'文件出现未知的文件编码: {e}')
    return None

if __name__ == '__main__':
    logging.basicConfig(filename = 'log.log', level = logging.DEBUG, format = ' %(asctime)s - %(levelname)s - %(message)s')
    #logging.disable(logging.CRITICAL)
    print(readFile(r'E:\projects\MySQL语言.sql'))
